package cz.muni.fi.pa036.kiss.daoimpl;

import cz.muni.fi.pa036.kiss.Entity.Zamestnanec;
import cz.muni.fi.pa036.kiss.dao.ZamestnanecDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author peter
 */
public class ZamestnanecDAOImpl implements ZamestnanecDAO{
    
    @PersistenceContext
    private EntityManager em;
    
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Override
    public void createZamestnanec(Zamestnanec zamestnanec) {
        if (zamestnanec == null) {
            throw new IllegalArgumentException("zamestnanec is null");
        }
        if (zamestnanec.getId() != null) {
            throw new IllegalArgumentException("zamestnanec.id is not null");
        }

        em.persist(zamestnanec);
    }

    @Override
    public Zamestnanec getZamestnanec(Long Id) {
        if (Id == null) {
            throw new IllegalArgumentException("null id");
        }
        Zamestnanec zamestnanec = em.find(Zamestnanec.class, Id);
        return zamestnanec;
    }

    @Override
    public void removeZamestnanec(Zamestnanec zamestnanec) {
        if (zamestnanec == null) {
            throw new IllegalArgumentException("zamestnanec is null");
        }
        if (zamestnanec.getId() == null) {
            throw new IllegalArgumentException("zamestnanec.id is null");
        }
        Zamestnanec c = em.find(Zamestnanec.class, zamestnanec.getId());
        em.remove(c);
    }

    @Override
    public void updateZamestnanec(Zamestnanec zamestnanec) {
        if (zamestnanec == null) {
            throw new IllegalArgumentException("zamestnanec is null");
        }
        if (zamestnanec.getId() == null) {
            throw new IllegalArgumentException("zamestnanec.id is null");
        }
        
        em.merge(zamestnanec);
    }

    @Override
    public List<Zamestnanec> findAll() {
        Query query = em.createQuery("SELECT c FROM Zamestnanec c");
        List<Zamestnanec> results = query.getResultList();

        return results;
    }

    @Override
    public List<Zamestnanec> findByName(String jmeno, String prijmeni) {
        boolean firstNameNull = (jmeno == null);
        boolean lastNameNull = (prijmeni == null);
        if (lastNameNull && firstNameNull) {
            throw new IllegalArgumentException("null jmeno and prijmeni");
        }

        Query query;
        if (firstNameNull) {
            query = em.createQuery("SELECT c FROM Zamestnanec c WHERE c.prijmeni like :prijmeni");
            query.setParameter("prijmeni", prijmeni);
        } else if (lastNameNull) {
            query = em.createQuery("SELECT c FROM Zamestnanec c WHERE c.jmeno like :jmeno");
            query.setParameter("jmeno", jmeno);
        } else {
            query = em.createQuery("SELECT c FROM Zamestnanec c WHERE c.jmeno like :jmeno AND c.prijmeni like :prijmeni");
            query.setParameter("prijmeni", prijmeni);
            query.setParameter("jmeno", jmeno);
        }

        List<Zamestnanec> results = query.getResultList();
        return results;
    }
}
